package com.newenergy.wisehome.gateway.account;


import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.newenergy.wisehome.R;
import com.newenergy.wisehome.config.BaseApp;
import com.newenergy.wisehome.data.bean.BLDeviceInfo;
import com.newenergy.wisehome.data.bean.Family;
import com.newenergy.wisehome.data.bean.UserInfoItem;
import com.newenergy.wisehome.data.ormlite.dao.BLDeviceInfoDao;
import com.newenergy.wisehome.data.ormlite.dao.FamilyDao;
import com.newenergy.wisehome.gateway.adapter.UserInfoAdapter;
import com.newenergy.wisehome.gateway.device.FamilyBLDeviceFragment;
import com.newenergy.wisehome.gateway.device.FamilyBLDeviceInfoFragment;
import com.newenergy.wisehome.gateway.device.PairDeviceFragment;
import com.newenergy.wisehome.gateway.family.BLFamilyListFragment;
import com.newenergy.wisehome.gateway.family.ManageBLFamilyFragment;
import com.newenergy.wisehome.gateway.manager.BLLocalFamilyManager;
import com.newenergy.wisehome.ui.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class GatewayManageFragment extends BaseFragment {

    @BindView(R.id.rv_user_info)
    EasyRecyclerView mRecyclerView;
    @BindView(R.id.tv_page)
    TextView mTvPage;
    private ArrayList<UserInfoItem> mData;
    private UserInfoAdapter mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_gateway_manage;
    }

    @Override
    public void init(View rootView) {
        mTvPage.setText("网关管理");
        mData = new ArrayList<>();
        mAdapter = new UserInfoAdapter(getContext());
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        mAdapter.setOnItemClickListener(position -> {
            UserInfoItem infoItem = mAdapter.getItem(position);
            BLLocalFamilyManager familyManager = BLLocalFamilyManager.getInstance();
            String blFamilyId = familyManager.mBLFamilyId;

            String itemTitle = infoItem.getTitle();
            if ("账号管理".equals(itemTitle)) {
                gotoManageAccount();
            } else if ("网关配置".equals(itemTitle)) {
                gotoPairDevice();
            } else if ("家庭管理".equals(itemTitle)) {
                gotoManageBlFamily();
            } else if ("BL家庭列表".equals(itemTitle)) {
                familyManager.queryFamilyAllInfo(blFamilyId);
                start(new BLFamilyListFragment());
            } else if ("当前家庭的BL设备".equals(itemTitle)) {
                if (TextUtils.isEmpty(blFamilyId)) {
                    toast("blFamilyId 为空");
                    return;
                }
                start(new FamilyBLDeviceFragment());
            } else if ("当前家庭的BL设备Info".equals(itemTitle)) {
                start(new FamilyBLDeviceInfoFragment());
            }
        });
    }

    /*** 去绑定或解绑家庭 */
    private void gotoManageBlFamily() {
        List<Family> familyList = new FamilyDao(getContext()).getFamiliesByType(1);
        if (familyList == null || familyList.size() < 1) {
            toast("没有本机创建的家庭,不能绑定网关");
            return;
        }
        for (Family family : familyList) {
            if (!TextUtils.isEmpty(family.getBlFamilyId())) {
                //有网关家庭
                start(new ManageBLFamilyFragment());
                return;
            }
        }
        List<BLDeviceInfo> blDeviceInfos = new BLDeviceInfoDao(getContext()).getTypeDevice(0);
        if (blDeviceInfos == null || blDeviceInfos.size() < 1) {
            toast("没有网关,请先去配置网关");
            return;
        }
        for (BLDeviceInfo blDeviceInfo : blDeviceInfos) {
            if (TextUtils.isEmpty(blDeviceInfo.getFamilyId())) {
                start(new ManageBLFamilyFragment());
                return;
            }
        }
        toast("没有未绑定的网关");
    }

    private void gotoPairDevice() {
        start(new PairDeviceFragment());
    }

    private void gotoManageAccount() {
        boolean isLogin = BLLocalFamilyManager.getInstance().isLogin;
        if (!isLogin) {
            startWithPop(new LoginFragment());
        } else {
            start(new UserInfoFragment());
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshListData();
    }

    private void refreshListData() {
        mData.clear();
        mAdapter.clear();
        boolean isLogin = BLLocalFamilyManager.getInstance().isLogin;
        String phone = isLogin ? BaseApp.getmBLUserInfoUnits().getPhone() : "未登录";
        mData.add(new UserInfoItem("账号管理", phone));
        mData.add(new UserInfoItem("网关配置", ""));
        mData.add(new UserInfoItem("家庭管理", ""));
        mData.add(new UserInfoItem("BL家庭列表", ""));
        mData.add(new UserInfoItem("当前家庭的BL设备", ""));
        mData.add(new UserInfoItem("当前家庭的BL设备Info", ""));
        mAdapter.addAll(mData);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.i(TAG, "onHiddenChanged: hidden = " + hidden);
        if (!hidden) {
            refreshListData();
        }
    }

    @OnClick({R.id.btn_back, R.id.tv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                doBack();
                break;
            case R.id.tv_back:
                doBack();
                break;
        }
    }
}
